package Game;

import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

/**
 * @program: pintu
 * @description:
 * @author: davidzhan
 * @create: 2025-04-01 11:07
 **/
public class GameJFrame extends JFrame implements KeyListener, ActionListener {
    int[][] data = new int[4][4];
    int x, y;//记录空白数组
    int steps = 0;
    JMenuItem reStartItem = new JMenuItem("重新开始");
    JMenuItem reLoginItem1 = new JMenuItem("重新登入");
    JMenuItem closeItem = new JMenuItem("关闭游戏");
    JMenuItem accountItem = new JMenuItem("公众号");
    JMenuItem mnItem = new JMenuItem("美女");
    JMenuItem animalItem = new JMenuItem("动物");
    JMenuItem sportsItem = new JMenuItem("运动");
    String path="image\\animal\\animal3\\";
    int[][]win={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,0}};
    public GameJFrame() {
        initJFrame();
        initJMenuBar();
        initData();
        initImage();
        this.setVisible(true);

    }

    private void initData() {
        int[] temp = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0};
        Random rand = new Random();
        for (int i = 0; i < 16; i++) {
            int index = rand.nextInt(16);
            int tempNum = temp[index];
            temp[index] = temp[i];
            temp[i] = tempNum;
        }
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                data[i][j] = temp[i * 4 + j];
            }
        }
        for (int i1 = 0; i1 < temp.length; i1++) {
            if (temp[i1] == 0) {
                x = i1 / 4;
                y = i1 % 4;
            }
        }
    }


    private void initImage() {
        this.getContentPane().removeAll();
        JLabel jLabel1 = new JLabel("步数" + steps);
        jLabel1.setBounds(20, 20, 100, 30);
        this.getContentPane().add(jLabel1);
        if(victory()){
            JLabel jLabel = new JLabel(new ImageIcon("image/win.png"));
            jLabel.setBounds(203, 283, 197, 73);
            this.getContentPane().add(jLabel);

        }
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                int num = data[i][j];
                System.out.println(num);
                JLabel label1 = new JLabel(new ImageIcon(path + num + ".jpg"));
                label1.setBounds(105 * j + 83, 105 * i + 134, 105, 105);
                label1.setBorder(new BevelBorder(1));//0表示让图片凸起，1表示让图片凹陷
                this.getContentPane().add(label1);
            }
        }
        ImageIcon imageIcon = new ImageIcon("image\\background.png");
        JLabel label = new JLabel(imageIcon);
        label.setBounds(40, 40, 508, 560);
        this.getContentPane().add(label);
        this.getContentPane().repaint();
    }


    private void initJMenuBar() {
        JMenuBar jMenuBar = new JMenuBar();
        JMenu functionjMenu = new JMenu("功能");
        JMenu aboutjMenu = new JMenu("关于我们");
        JMenu changejMenu = new JMenu("更换图片");
        changejMenu.add(mnItem);
        changejMenu.add(animalItem);
        changejMenu.add(sportsItem);
        functionjMenu.add(changejMenu);
        reStartItem.addActionListener(this);
        reLoginItem1.addActionListener(this);
        closeItem.addActionListener(this);
        accountItem.addActionListener(this);
        mnItem.addActionListener(this);
        animalItem.addActionListener(this);
        sportsItem.addActionListener(this);
        functionjMenu.add(reStartItem);
        functionjMenu.add(reLoginItem1);
        functionjMenu.add(closeItem);
        aboutjMenu.add(accountItem);
        jMenuBar.add(functionjMenu);
        jMenuBar.add(aboutjMenu);
        this.setJMenuBar(jMenuBar);
    }

    private void initJFrame() {
        this.setTitle("拼图游戏");
        this.setSize(600, 700);
        this.setAlwaysOnTop(true);//置顶
        this.setLocationRelativeTo(null);//取消居中
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setLayout(null); //取消默认布局
        this.addKeyListener(this);
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode();
        if(keyCode == 65){
            this.getContentPane().removeAll();
            JLabel jLabel = new JLabel(new ImageIcon("image/animal/animal3/all.jpg"));
            jLabel.setBounds(0,0,600,700);
            this.getContentPane().add(jLabel);
            ImageIcon imageIcon = new ImageIcon("image\\background.png");
            JLabel label = new JLabel(imageIcon);
            label.setBounds(40, 40, 508, 560);
            this.getContentPane().add(label);
            this.getContentPane().repaint();
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
        if(victory()){
            return;
        }
        int keyCode = e.getKeyCode();
        if(keyCode==87){
            data= new int[][]{{1,2,3,4}, {5,6,7,8}, {9,10,11,12}, {13,14,15,0}};
            initImage();
        }
        switch (keyCode) {
            case 37:
                if (x == 3) {
                    break;
                }
                data[x][y] = data[x + 1][y];
                data[x + 1][y] = 0;
                x++;
                steps++;
                initImage();
                break;
            case 38:
                if (y == 3) {
                    break;
                }
                data[x][y] = data[x][y + 1];
                data[x][y + 1] = 0;
                y++;
                steps++;

                initImage();
                break;
            case 39:
                if (x == 0) {
                    break;
                }
                data[x][y] = data[x - 1][y];
                data[x - 1][y] = 0;
                x--;
                steps++;
                initImage();
                break;
            case 40:
                if (y == 0) {
                    break;
                }
                data[x][y] = data[x][y - 1];
                data[x][y - 1] = 0;
                y--;
                steps++;
                initImage();
                break;
            default:
                System.out.println("无效按键");
                break;

        }
        if(keyCode==65){
            initImage();

        }


    }
    public boolean victory(){
        for(int i=0;i<4;i++){
            for(int j=0;j<4;j++){
                if(data[i][j]!=win[i][j]){
                    return false;
                }
            }
        }
        return true;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        Random random = new Random();
        if(source==reStartItem){
            System.out.println("重新开始");
            initData();
            steps=0;
            initImage();
        }else if (source == reLoginItem1){
            System.out.println("重新登入");
            this.setVisible(false);
            new LoginJFrame();

        }else if(source==closeItem){
            System.out.println("关闭游戏");
            System.exit(0);

        }else if(source == accountItem){
            System.out.println("公众号");
            JDialog dialog = new JDialog();
            JLabel jLabel =new JLabel(new ImageIcon("image/about.png"));
            jLabel.setBounds(0,0,258,258);
            dialog.getContentPane().add(jLabel);
            dialog.setSize(400,300);
            dialog.setAlwaysOnTop(true);
            dialog.setLocationRelativeTo(null);
            dialog.setModal(true);
            dialog.setVisible(true);
        }else if(source == mnItem){
            System.out.println("美女");
            path="image\\girl\\girl"+random.nextInt(13)+"\\";
            steps=0;
            initImage();

        }else if(source == animalItem){
            System.out.println("动物");
            path="image\\animal\\animal"+random.nextInt(8)+"\\";
            steps=0;
            initImage();
        }else if(source==sportsItem){
            System.out.println("运动");
            path="image\\sport\\sport"+random.nextInt(10)+"\\";
            steps=0;
            initImage();
        }

    }
}
